from db.dao import PositionRecordDao
from db.models import StockPositionRecord
from quantification.data_handler.DataHandler import DataHandler
from quantification.data_handler.Order import Order
from quantification.data_handler.Position import Position
from utils.util import ObjDictTool


class PositionRecord(DataHandler):
    '''
    仓位变动记录表
    '''

    __slots__ = ('stock_code', 'stock_name', 'average_price', 'buy_time', 'hold_quantity',
                 'update_time', 'position_id')



    def __init__(self, position: Position=None, order: Order=None):
        if position:

            self.position_id = position.position_id

            self.stock_code = position.stock_code

            self.stock_name = position.stock_name
        else:
            self.position_id = None

            self.stock_code = None

            self.stock_name = None
        if order:

            self.order_id = order.order_id

            # 交易方向1：买入，-1：卖出
            self.deal_direction = order.deal_direction

            # 股票交易价格
            self.price = order.price

            # 交易数量
            self.amount = order.plans
        else:
            self.order_id = None

            # 交易方向1：买入，-1：卖出
            self.deal_direction = None

            # 股票交易价格
            self.price = None

            # 交易数量
            self.amount = None
        # 创建时间
        self.create_time = None

    def load_data(self, start_date=None, end_date=None):
        """
        返回最近更新的数据条目
        """
        positionRecords = PositionRecordDao().queryByParam(self.position_id, None, None)

    def store_data(self):
        """
        保存数据
        """
        position_record = StockPositionRecord()
        ObjDictTool.copyA2B(self, position_record)
        PositionRecordDao().save(position_record)

    def calculate_buy_amount(self):
        '''
        计算买入金额
        '''
        pass

    def calculate_cur_amount(self):
        '''
        计算当前持仓金额
        '''
        pass

    def calculate_return(self):
        '''
        计算持仓收益
        '''
        pass
